#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 110;

int n;
int num[N];

void p(){
    for(int i = 1; i <= n; i ++){
        cout << num[i] << " ";
    }
    cout << '\n';
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
  
        cin >> n;
        for(int i = 1; i <= n; i ++){
            num[i] = i;
        }

        cout << n << '\n';

        p();
        swap(num[1], num[n]);
        p();
        for(int i = 2; i < n; i ++){
            swap(num[i], num[n]);
            p();
        }

    }


    return 0;
}